<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Post\Recycle;
use App\Models\Goods;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class SysGoodsController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '商品';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {

        $grid = new Grid(new Goods());

        $grid->model()->orderByDesc('id');


        $grid->column('id', __('ID'));
        $grid->column('goods_name', __('商品名称'));
        $grid->column('goods_prices', __('商品价格'))->display(function($goods_prices){
            return '¥'.$goods_prices;
        });
        $grid->column('goods_cover', __('商品封面'))->lightbox(['width' => 50, 'height' => 50]);

        // 设置text、color、和存储值
        $states = [
            'on' => ['value' => 1, 'text' => '展示中', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '隐藏中', 'color' => 'danger'],
        ];
        $grid->column('is_show', __('展示/隐藏'))->switch($states);
        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('修改时间'));

        $grid->disableExport();

        $grid->actions(function ($actions) {
            // 去掉查看
            $actions->disableView();

        });


        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Goods::findOrFail($id));

        $show->field('id', __('Id'));
//        $show->field('goods_name', __('Goods name'));
//        $show->field('goods_prices', __('Goods prices'));
//        $show->field('goods_cover', __('Goods cover'));
//        //$show->field('pictures', __('Goods details'));
//        $show->field('is_show', __('Is show'));
//        $show->field('created_at', __('Created at'));
//        $show->field('updated_at', __('Updated at'));
//        $show->field('deleted_at', __('Deleted at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Goods());

        $form->text('goods_name', __('商品名称'))->rules('required' , [
            'required' => '商品名称 不能为空',
        ])->setWidth(6,2);

        $form->currency('goods_prices', __('商品价格'))->rules('required|numeric|min:0.01|max:999' , [
            'required' => '商品价格 不能为空',
            'min' => '商品价格 不能小于 0.01',
            'max' => '商品价格 不能大于 999'
        ])->symbol('￥');

        $form->image('goods_cover', __('商品封面'))->resize(800, 800)->uniqueName()->rules('required', [
            'required' => '商品封面 不能为空',
        ])->setWidth(6,2)->help('建议图片大小 800*800 默认压缩为 800*800');

//        $form->multipleImage('goods_details', __('商品详情'))->sortable()->uniqueName()->removable()->setWidth(6,2)->help('保存之后可排序/删除');
  //      $form->multipleImage('pictures')->sortable();
        $form->multipleImage('pictures', __('商品详情'))->uniqueName()->removable()->setWidth(6,2);

        // 设置text、color、和存储值
        $states = [
            'on' => ['value' => 1, 'text' => '展示中', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '隐藏中', 'color' => 'danger'],
        ];
        $form->switch('is_show', __('是否展示'))->states($states);

        return $form;
    }
}
